

#include <stdio.h>

#include "bus.h"
#include "addressmap.cpp"

#define CONFIG_SOC_DM365
#include "hardware.h"
#include "davinci-timer.h"
#include "davinci-intc.h"
#include "serial-port.h"
#include "davinci.h"
#include "davinci-emac.h"
#include "davinci-psc.h"
TAddressMap<unsigned int> bus;

#define ROM_TOP 0x400000

__u32 bus_read_int(__u32 address)
{
	//if(address<ROM_TOP) 
	//{
	//	address+=0xbf080000;
	//}

	//if(address==0x800009d8) 
	//printf("");
	return bus.ReadDWord(address);
}

__u16 bus_read_short(__u32 address)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	return bus.ReadWord(address);
}
__u8 bus_read_byte (__u32 address)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	return bus.ReadByte(address);
}

void bus_write_byte (__u32 address,__u8  dat)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	 bus.WriteByte(address,(char)dat);
}

void bus_write_short(__u32 address,__u16 dat)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	 bus.WriteWord(address,dat);
}
void bus_write_int(__u32 address,__u32 dat)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	//if(address==(0x80004000+4*0xfff)) 

	 bus.WriteDWord(address,dat);
}


void bus_read_line(__u32 address,__u8 * buffer)
{
	//if(address<ROM_TOP) 
	//{
	//	address+=0xbf080000;
	//}
	assert((address&0x1f)==0);

	bus.ReadString(address,(char*)buffer,32);
}
void bus_write_line(__u32 address,__u8 * buffer)
{
	//if(address<ROM_TOP) 
	//{
	//	address+=0xbf080000;
	//}
	assert((address&0x1f)==0);

	bus.WriteString(address,(char*)buffer,32);
}

void bus_read_bytes(__u32 address,__u8 * buffer,__u32 bytes)
{
	//if(address<ROM_TOP) address+=0xbf080000;

	bus.ReadString(address,(char*)buffer,bytes);
}
void bus_write_bytes(__u32 address,__u8 * buffer,__u32 bytes)
{
	//if(address<ROM_TOP) address+=0xbf080000;
	bus.WriteString(address,(char*)buffer,bytes);
}

 
__u8 bus_inta()
{
	return 0;
}

int board_init()
{


	//
	// boot loader @ 0
	//
	//bus.RegisterAddressMapToFile(L"\\arm-linux\\u-boot.bin",0,0,0);
	//
	// xip image @0x80000
	bus.RegisterAddressMapToFile(L"\\arm-linux\\xipImage",0,0,0xbf080000);
	//
	// ram,128M @1M
	//
	bus.RegisterAddressMapToFileRW(L"\\arm-linux\\ram.bin",0,0x8000000,0x80000000);
	//
	// uart
	//
	void *p=bus.RegisterAddressRange(serial_port_handler,
		DAVINCI_UART0_BASE,
		SERIAL_PORT_ADDR_SIZE,1);

	if(p)
	{
		*(char *)p=0;
	}
	
	p=bus.RegisterAddressRange(serial_port_handler,
		DAVINCI_UART1_BASE,
		SERIAL_PORT_ADDR_SIZE,1);

	if(p)
	{
		*(char *)p=1;
	}
	//
	// timer
	//
	p=bus.RegisterAddressRange(davinci_timer_handler,
		DAVINCI_TIMER0_BASE,
		DAVINCI_TIMER_ADDR_SIZE,1);
	if(p)
	{
		*(char *)p=0;
	}
	p=bus.RegisterAddressRange(davinci_timer_handler,
		DAVINCI_TIMER1_BASE,
		DAVINCI_TIMER_ADDR_SIZE,1);
	if(p)
	{
		*(char *)p=1;
	}
	//
	// emac mdio regs
	//
	p=bus.RegisterAddressRange(davinci_emac_handler,
		EMAC_MDIO_BASE_ADDR,
		EMAC_MDIO_REG_SIZE,1);
	if(p)
	{
		*(char *)p=0;
	}
	//
	// emac ewraps
	//
	p=bus.RegisterAddressRange(davinci_emac_handler,
		EMAC_WRAPPER_BASE_ADDR,
		EMAC_WRAPPER_REG_SIZE,1);
	if(p)
	{
		*(char *)p=1;
	}


	//
	// emac mdio regs
	//
	p=bus.RegisterAddressRange(davinci_emac_handler,
		EMAC_WRAPPER_RAM_ADDR,
		EMAC_WRAPPER_RAM_SIZE,1);
	if(p)
	{
		*(char *)p=2;
	}
	//
	// arm intc
	//
	p=bus.RegisterAddressRange(davinci_intc_handler,
		DAVINCI_ARM_INTC_BASE,
		DAVINCI_INTC_REG_SIZE,0);

	//
	// psc
	//
		p=bus.RegisterAddressRange(davinci_psc_handler,
		DAVINCI_PWR_SLEEP_CNTRL_BASE,
		0xc00,0);

	return 0;
}


void board_do_clock()
{
	davinci_timer_do_clock();
}